package com.woniu.daiquhua.web;

import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;
import lombok.Data;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class zhenziSMS {

    private String apiUrl="https://sms_developer.zhenzikj.com";
    private String appId = "110002";
    private String appSecret = "d5591add-71f5-41ef-865b-2984b644048a";


    @RequestMapping(value = "/phone/code")
    public void getCode(@RequestParam("phoneNumber") String phoneNumber, HttpServletRequest request, HttpServletResponse resp) {
        try{
            ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
            String code = String.valueOf(new Random().nextInt(899999) + 100000);
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("number", phoneNumber);
            params.put("templateId", "6768");
            //这个参数就是短信模板上那两个参数
            String[] templateParams = new String[2];
            templateParams[0] = code;
            templateParams[1] = "1分钟";
            params.put("templateParams", templateParams);
            String result = client.send(params);

            JSONObject json = null;
            json = JSONObject.parseObject(result);
            PrintWriter out = resp.getWriter();
            if (json.getIntValue("code")!=0){//发送短信失败
                out.write("no");
                return;
            }
            //将验证码存到session中,同时存入创建时间
            //以json存放，这里使用的是阿里的fastjson
            json = new JSONObject();
            json.put("memPhone",phoneNumber);
            json.put("code",code);
            json.put("createTime",System.currentTimeMillis());
            // 将认证码存入SESSION
            request.getSession().setAttribute("code",json);
            out.write("ok");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/phone/verifyCode")
    public void verifyCode(@RequestParam("phonVerify") String phonVerify, HttpServletRequest request, HttpServletResponse resp) throws IOException {
        HttpSession session = request.getSession(false);
        JSONObject userCode = (JSONObject)session.getAttribute("code");
        //验证码
        String code = (String) userCode.get("code");
        PrintWriter out = resp.getWriter();
        if (code.equals(phonVerify)){
            out.write("ok");
        }else {
            out.write("no");
        }
    }

}